<template>
  <div class="container">
    <el-dialog
      :title="text === 'add' ? '新增文章' : '修改文章'"
      :visible.sync="dialogVisible"
      width="55%"
      :before-close="handleClose"
    >
      <el-form
        :label-position="labelPosition"
        label-width="80px"
        :model="form"
        ref="form"
        :rules="rules"
      >
        <el-form-item label="文章标题" prop="title">
          <el-input
            v-model="form.title"
            placeholder="请输入文章标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="文章内容" class="roles" prop="articleBody">
          <quillEditor v-model="form.articleBody" class="role" ></quillEditor>
        </el-form-item>
        <el-form-item label="视频地址">
          <el-input
            v-model="form.videoURL"
            placeholder="请输入视频地址"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-row>
        <el-col :span="24" :push="19">
          <el-button size="medium" @click="handleClose">取消</el-button>
          <el-button type="primary" size="medium" @click="submit"
            >确认</el-button
          >
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 富文本框
import { quillEditor } from 'vue-quill-editor'

import { add, update } from '@/api/hmmm/articles.js'
export default {
  components: {
    quillEditor
  },
  props: {
    text: {
      type: String
    }
  },
  data () {
    return {
      labelPosition: 'right',
      form: {
        name: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        articleBody: [
          { required: true, message: '请输入文章标题', trigger: 'blur' }
        ]
      },
      dialogVisible: false
    }
  },
  methods: {
    // 关闭弹窗
    handleClose () {
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        articleBody: '',
        videoURL: ''
      }
      this.dialogVisible = false
    },
    // 提交事件
    async submit () {
      try {
        await this.$refs.form.validate()
        if (this.text === 'add') {
          const data = await add(this.form)
          console.log(data)
          this.$parent.getList()
          this.handleClose()
        } else {
          await update(this.form)
          // console.log(data)
          this.$parent.getList()
          this.handleClose()
        }
      } catch (error) {
        console.log(error)
      }
    }

  }
}
</script>

<style scoped lang="less">
.roles {
  height: 300px;
  .role {
    height: 200px;
  }
}
</style>
